define( [
  'jquery',
  'require',
  'ueditorCore',
  'a92d301d77',
  'message'
], function( $ ) {
  // ===============================================
  if ( location.hostname.indexOf( 'bigwe.com' ) > -1 ) document.domain = 'bigwe.com';
  // ===============================================


  var ShowTitleMessage = require( 'message' );
  var showTime = ShowTitleMessage.showTime;

  var replaceClassArray = [ '._135editor', '.wx96Diy', '.Powered-by-XIUMI', '.tn-Powered-by-XIUMI', '.RankEditor', '.xmyeditor', '.yead_editor', '[label="Copyright © 2016 playhudong All Rights Reserved."]' ];
  var backgroundClassArray = [ '96wx-bgpic', 'xmy-bdpic', 'wx-bg' ];


  var returnParam = {};

  returnParam.getEditor = function() {
    var editor = UE.getEditor.apply( UE, arguments );
    editorInit( editor );
    return window.current_editor = editor;
  }

  function editorInit( current_editor ) {
    window.BASEURL = "http://localhost";

    $( '.js_dynamicHeight' ).height( $( window ).height() - 155 );

    current_editor.ready( function() {
      $( '.edui-editor-toolbarbox .js_tooltip, .edui-editor-toolbarbox .edui-button-body' ).tooltip( {
        'container': 'body',
        'placement': 'top',
        'title': function() {
          return $( this ).prop( 'title' ) || $( this ).data( 'tooltip' );
        }
      } );
    } );

    current_editor.addListener( 'focus', function() {
      $( '.body-tips', this.container ).hide();
      if ( !$( '.sp-container' ).hasClass( 'sp-hidden' ) ) {
        $( '#js_color_anchor' ).trigger( 'click' );
      }
    } );

    current_editor.addListener( 'blur', function() {
      var content = this.getContent();

      if ( !content ) {
        $( '.body-tips', this.container ).show();
      }
      this.clean_135helper();
    } );
    $( document ).on( 'click.clean_135helper', function() {
      current_editor.clean_135helper();
    } )

    current_editor.addListener( 'selectionchange', function() {
      if ( this.getContent() ) {
        $( '.body-tips', this.container ).hide();
      }
    } );

    current_editor.addListener( 'contentChange', function() {
      $( '.body-tips', this.container ).hide();
      // 移除微知logo
      $( '[bigwe-from="weizhi"]', this.body ).remove();

      replaceClassArray.map( function( a ) {
        $( a, this.body ).addClass( '135editor' );
      }.bind( this ) );

      backgroundClassArray.map( function( a ) {
        $( '.' + a, this.body ).addClass( '135bg' ).removeClass( a );
      }.bind( this ) );

    } );

    $( document ).on( 'click', '.body-tips', function() {
      current_editor.focus();
    } );

    var UEUtils = $.extend( UE.utils, {
      imgsrcAttr: function() {
        $( 'img', this ).each( function() {
          // prop attr  prop会取的带有域名的前缀  attr原样返回
          var $src = $( this ).attr( 'src' );
          if ( $src ) {
            $( this ).prop( 'src', wechartImgCheck( $src ) );
          } else {
            var $datasrc = $( this ).attr( 'data-src' );
            if ( $datasrc ) {
              $( this ).prop( 'src', wechartImgCheck( $datasrc ) );
            }
          }
        } );
        $( 'iframe', this ).each( function() {
          var $datasrc = $( this ).attr( 'data-src' );
          if ( $datasrc ) {
            $( this ).prop( 'src', $datasrc );
          }
        } );

        $( 'section,div,blockquote,span', this ).each( function() {
          var bgImg = $( this ).css( 'background-image' );
          if ( bgImg && bgImg != 'none' ) {
            bgImg = bgImg.substr( 0, bgImg.lastIndexOf( '")' ) ).replace( 'url("', '' );
            $( this ).css( {
              'background-image': 'url(' + wechartImgCheck( bgImg, !0 ) + ')'
            } )
          }
        } );
        return this;
      }
    } );

    function setDraft() {
      current_editor.execCommand( 'drafts' );
    }
    $( '#custom-color-text' ).colorPicker( {
      customBG: '#FFF',
      size: 2,
      appenTo: 'body',
      mode: 'hsv-h', //rgb-r
      init: function( elm, colors ) { // colors is a different instance (not connected to colorPicker)
        elm.style.backgroundColor = elm.value;
        elm.style.color = colors.rgbaMixCustom.luminance > 0.22 ? '#222' : '#ddd';
      }
    } ).blur( function() {
      setBackgroundColor( this.value, '#FFF', true );
      this.style.backgroundColor = this.value;
      this.style.color = '#FFF';
    } ).keyup( function() {
      if ( this.value.search( '#' ) == 0 ) {
        if ( this.value.length == 7 ) { //|| this.value.length ==4
          $( this ).trigger( 'focus.colorPicker' );
        }
      } else {
        //alert(this.value.search('rgb'));alert(this.value.indexOf(")"));
        if ( this.value.search( 'rgb' ) == 0 && this.value.indexOf( ")" ) > 0 ) {
          $( this ).trigger( 'focus.colorPicker' );
        }
      }
    } );
    //图片圆形
    $( document ).on( 'click', '#set-image-radius', function() {
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      obj.find( 'img' ).css( 'border-radius', '50%' ); //图片
      obj.find( '.135bg' ).css( 'border-radius', '50%' ); //背景图片
      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '#set-image-border', function() {
      //图片边框
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      obj.find( 'img' ).css( {
        "background-color": "#fff",
        "border-radius": "4px",
        "max-width": "100%",
        "padding": "4px",
        "border": "1px solid #ddd"
      } );
      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '#flat-strip-radius', function() {
      //去圆角
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      obj.find( '*' ).css( 'border-radius', '' );
      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '#flat-add-radius', function() {
      //加圆角
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      obj.find( '*' ).each( function() {
        if ( $( this ).css( 'background-color' ) != 'transparent' && $( this ).css( 'background-color' ) != '' ) {
          $( this ).css( 'border-radius', '4px' );
        } else {
          $( this ).css( 'border-radius', '4px' );
        }

      } );
      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '#flat-strip-border', function() {
      //去边框
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );

      obj.find( '*' ).each( function() {
        $( this ).css( 'border', '' );
        $( this ).css( 'border-width', '0' );
      } );

      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '#flat-strip-shadow', function() {
      //去阴影
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      obj.find( '*' ).css( 'box-shadow', '' );
      var newHtml = obj.html();
      setDealingHtml( newHtml );
      obj = null;
    } ).on( 'click', '#flat-add-shadow', function() {
      //加阴影
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );

      obj.find( '*' ).each( function() {
        if ( $( this ).css( 'background-color' ) == 'transparent' || $( this ).css( 'background-color' ) == '' ) {} else {
          $( this ).css( 'box-shadow', 'rgba(205, 205, 205,0.9) 2px 3px 2px' );
        }
      } );
      obj.find( 'hr' ).each( function() {
        $( this ).css( 'box-shadow', 'rgba(205, 205, 205,0.9) 1px 1px 2px' );
      } )
      var newHtml = obj.html();
      setDealingHtml( newHtml );
      obj = null;
    } ).on( 'click', '#135-random-transform', function() {
      //随机倾斜
      current_active_135item = current_editor.currentActive135Item();
      if ( current_active_135item ) {
        var deg = parseInt( Math.random() * 8 ); // 正负8度
        var f = Math.random() * 10 > 5 ? '' : '-';
        var transform = 'rotateZ(' + f + deg + 'deg)';

        setElementTransform( $( current_active_135item ).get( 0 ), transform );
      } else {
        showErrorMessage( "请在内容中选择要倾斜的样式" );
      }
    } ).on( 'click', '#135-random-color', function() {
      //随机换色
      var html = getDealingHtml();
      var obj = $( '<div>' + html + '</div>' );
      var bgcolors = [ '#5BB75B', '#2E8BCC', '#F09609', '#E51400', '#7B4F9D', '#E671B8', '#008641', '#20608E', '#FFC40D' ];

      var rd = Math.floor( Math.random() * ( bgcolors.length ) );
      var used = [];
      var current_bgcolor = bgcolors[ rd ];
      if ( obj.find( '.135editor' ).size() > 0 ) {
        var components = obj.find( '.135editor' ).each( function( i ) {
          if ( i % random_color_step == 0 ) {
            do {
              rd = Math.floor( Math.random() * ( bgcolors.length ) );
            }
            while ( jQuery.inArray( rd, used ) != -1 );

            used[ used.length ] = rd;
            if ( used.length == bgcolors.length ) {
              used = [];
            }

            current_bgcolor = bgcolors[ rd ];
          }
          $( this ).html( parseHtml( $( this ).html(), current_bgcolor, '#FFF' ) );
        } );
      } else {
        $( obj ).html( parseHtml( $( obj ).html(), current_bgcolor, '#FFF' ) );
      }

      setDealingHtml( obj.html() )
      obj = null;
    } ).on( 'click', '.color-swatch', function() {

      $( '.color-swatch' ).removeClass( 'active' );
      $( this ).addClass( 'active' );
      var color = $( this ).data( 'color' ); //data-color为前景色，bgcolor为背景色，或者无背景文字的前景色
      var bgcolor = $( $( this ) ).getHexBackgroundColor()
      $( '#custom-color-text' ).val( bgcolor ).css( 'backgroundColor', bgcolor );

      if ( !color ) color = '#FFF';
      setBackgroundColor( bgcolor, color, true );
      if ( replace_full_color == false && !current_editor.currentActive135Item() ) {
        parseObject( $( '.editor-template-list' ), bgcolor, color );
      }
    } );

    $( '#right-fix-tab > li > a' ).click( function() {
      $( this ).parent().addClass( "selected" ).siblings().removeClass( "selected" );
      $( '.tab-con-div>div' ).hide();
      $( '.tab-con-div>div' ).eq( $( this ).parent().index() ).show();
    } );

    $.fn.getHexBackgroundColor = function() {
      var rgb = $( this ).css( 'background-color' );
      rgb = rgb.match( /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/ );

      function hex( x ) {
        return ( "0" + parseInt( x ).toString( 16 ) ).slice( -2 );
      }
      return rgb = "#" + hex( rgb[ 1 ] ) + hex( rgb[ 2 ] ) + hex( rgb[ 3 ] );
    }


    $( document ).on( 'change', '#replace-color-all,.sp-container .js-replace-color-all', function() {
      if ( this.checked ) {
        replace_full_color = true;
      } else {
        replace_full_color = false;
      }
    } );
  }


  return returnParam;
} );
